package com.kolon.business.mAccessMan.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jdom.Document;
import org.jdom.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mAccessMan.controller.MAccessManApplyViewController")
public class MAccessManApplyViewController implements SimpleController {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value="/business/mAccessMan/mAccessManApplyView.do")
	public ModelAndView controller(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 도메인
		String url = MsgUtil.getMessage("xml.url");
		// 세션 아이디 및 암호 값을 불러옴
		HttpSession httpSession = request.getSession();
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
		String id = (String)userMap.get("userId");
		String name = (String)userMap.get("UserName");
		String pw = (String)userMap.get("userPw");
		String companyCD = (String)userMap.get("CompanyCD");
		String deptCode = (String)userMap.get("DeptCode");	
		String Mode = ComUtil.nullChk(request.getParameter("Mode"));
		ModelAndView modelAndView = new ModelAndView();
		// 요청 등록 처리
		if(Mode.equals("Save")) {
			StringBuffer buffer2 = new StringBuffer();
			buffer2.append(url);
			buffer2.append("/access_man.aspx?command=").append("update");
			buffer2.append("&APPLY_SEQ=").append(ComUtil.nullChk(request.getParameter("Apply_Seq")));
			buffer2.append("&BUSINESS_CODE=").append(ComUtil.nullChk(request.getParameter("sel_business_code")));
			buffer2.append("&ReqUserID=").append(id);
			buffer2.append("&ReqUserName=").append(name);
			buffer2.append("&ACCESS_AUTH_CODE=").append(ComUtil.nullChk(request.getParameter("sel_access_auth_code")));
			buffer2.append("&Customer=").append(ComUtil.nullChk(request.getParameter("txt_visitor_name")));
			buffer2.append("&CusComName=").append(ComUtil.nullChk(request.getParameter("txt_visitor_comp_name")));
			buffer2.append("&CusDeptName=").append("");
			buffer2.append("&CusTel=").append(ComUtil.nullChk(request.getParameter("txt_visitor_tel_no")));
			buffer2.append("&REASON=").append(ComUtil.getEncode(request.getParameter("txt_visitor_reason"),"EUC-KR"));
			buffer2.append("&VISITOR_PER_CNT=").append(ComUtil.nullChk(request.getParameter("visitor_per_cnt")));
			buffer2.append("&RsvDate=").append(ComUtil.nullChk(request.getParameter("txt_reservation_date")));
			buffer2.append("&START_TIME=").append(ComUtil.nullChk(request.getParameter("txt_reservation_start_hour"))+":"+ComUtil.nullChk(request.getParameter("txt_reservation_start_minute")));
			buffer2.append("&END_TIME=").append(ComUtil.nullChk(request.getParameter("txt_reservation_end_hour"))+":"+ComUtil.nullChk(request.getParameter("txt_reservation_end_minute")));
			buffer2.append("&CompanionDetail=").append(request.getParameter("CompanionDetail"));
			// XML DOCUMENT 및 인증 처리
			ComUtil.getDocument(buffer2.toString(), id, pw);
			//String message_url = "/business/mAccessMan/mAccessManApplyView.do?ApplySeq="+ComUtil.nullChk(request.getParameter("Apply_Seq"));
			String message_url = "/business/mAccessMan/mAccessManApplyList.do";
			String scriptType 	= "003";
			String message   	= "예약을 수정하였습니다.";
			modelAndView.addObject("scriptType", scriptType);
			modelAndView.addObject("message", message);
			modelAndView.addObject("message_url", message_url);
			modelAndView.setViewName("/business/common/alert");
		}else{
			String BUSINESS_CODE = "";
			StringBuffer buffer = new StringBuffer();
			buffer.append(url);
			Map map = new HashMap();
			// 처리 상세보기(기본 값)
			buffer.append("/access_man.aspx?command=").append("content");
			buffer.append("&id=").append(ComUtil.nullChk(request.getParameter("ApplySeq")));
			buffer.append("&userid=").append(id);
			// XML DOCUMENT 및 인증 처리
			Document doc = ComUtil.getDocument(buffer.toString(), id, pw);
			Element element = doc.getRootElement();
			List xmlList = element.getChildren("dept");
			Iterator itXml = xmlList.iterator();
			// XML 데이터 
			if (itXml.hasNext()) {
				Element el = (Element)itXml.next();
				map = new HashMap();
				map.put("APPLY_SEQ", el.getChild("APPLY_SEQ").getText());
				map.put("BUSINESS_CODE", el.getChild("BUSINESS_CODE").getText()); 
				BUSINESS_CODE = el.getChild("BUSINESS_CODE").getText();
				map.put("ACCESS_AUTH_CODE", el.getChild("ACCESS_AUTH_CODE").getText()); 
				map.put("APPLY_EMP_ID", el.getChild("APPLY_EMP_ID").getText()); 		
				map.put("APPLY_EMP_NAME", el.getChild("APPLY_EMP_NAME").getText());
				map.put("VISITOR_STATUS", el.getChild("VISITOR_STATUS").getText());
				map.put("VISITOR_NAME", el.getChild("VISITOR_NAME").getText()); 
				map.put("VISITOR_COMP_NAME", el.getChild("VISITOR_COMP_NAME").getText());
				map.put("VISITOR_DEPT_NAME", el.getChild("VISITOR_DEPT_NAME").getText());
				if (el.getChild("VISITOR_TEL_NO") != null)
				map.put("VISITOR_TEL_NO", el.getChild("VISITOR_TEL_NO").getText());
				else map.put("VISITOR_TEL_NO", "");
				map.put("VISITOR_REASON", el.getChild("VISITOR_REASON").getText());
				map.put("VISITOR_TEL_NO", el.getChild("VISITOR_TEL_NO").getText());
				map.put("VISITOR_PER_CNT", el.getChild("VISITOR_PER_CNT").getText());
				map.put("RESERVATION_DATE", el.getChild("RESERVATION_DATE").getText());			
				map.put("RESERVATION_START_HH", ComUtil.sToi(ComUtil.nullChk(el.getChild("RESERVATION_START_HH").getText(),"0")));
				map.put("RESERVATION_START_MM", ComUtil.sToi(ComUtil.nullChk(el.getChild("RESERVATION_START_MM").getText(),"0")));
				map.put("RESERVATION_END_HH", ComUtil.sToi(ComUtil.nullChk(el.getChild("RESERVATION_END_HH").getText(),"0")));
				map.put("RESERVATION_END_MM", ComUtil.sToi(ComUtil.nullChk(el.getChild("RESERVATION_END_MM").getText(),"0")));
			}	
			// 동행자 정보
			Document doc2 = ComUtil.getDocument(buffer.toString(), id, pw);
			Element element2 = doc2.getRootElement();
			List xmlList2 = element2.getChildren("dept1");
			Iterator itXml2 = xmlList2.iterator();
			List mapList2 = new ArrayList();
			Map map2 = new HashMap();
			// XML 데이터 
			while (itXml2.hasNext()) {
				Element el = (Element)itXml2.next();
				map2 = new HashMap();
				map2.put("VISITOR_DETAIL_SEQ", el.getChild("VISITOR_DETAIL_SEQ").getText());
				map2.put("VISITOR_NAME", el.getChild("VISITOR_NAME").getText());
				map2.put("VISITOR_TEL_NO", el.getChild("VISITOR_TEL_NO").getText());
				map2.put("APPLY_SEQ", el.getChild("APPLY_SEQ").getText());
				mapList2.add(map2);
			}
			// 장소 1 리스트
			StringBuffer buffer3 = new StringBuffer();
			buffer3.append(url);
			buffer3.append("/access_man.aspx?command=").append("place");
			buffer3.append("&companyCD=").append(companyCD);
			buffer3.append("&deptCD=").append(deptCode);
			// XML DOCUMENT 및 인증 처리
			Document doc3 = ComUtil.getDocument(buffer3.toString(), id, pw);
			Element element3 = doc3.getRootElement();
			List xmlList3 = element3.getChildren("dept");
			Iterator itXml3 = xmlList3.iterator();
			List mapList3 = new ArrayList();
			Map map3 = new HashMap();
			// XML 데이터 
			while (itXml3.hasNext()) {
				Element el = (Element)itXml3.next();
				map3 = new HashMap();
				map3.put("BUSINESS_CODE", el.getChild("BUSINESS_CODE").getText());
				map3.put("BUSINESS_NAME", el.getChild("BUSINESS_NAME").getText());
				mapList3.add(map3);
			}		
			
			modelAndView.addObject("mapList3", mapList3); //장소1
			// 장소2 리스트
			StringBuffer buffer4 = new StringBuffer();
			buffer4.append(url);
			buffer4.append("/access_man.aspx?command=").append("place2"); //5-2가 영역임
			buffer4.append("&BUSINESS_CODE=").append(BUSINESS_CODE);
			// XML DOCUMENT 및 인증 처리
			Document doc4 = ComUtil.getDocument(buffer4.toString(), id, pw);
			Element element4 = doc4.getRootElement();
			List xmlList4 = element4.getChildren("dept");
			Iterator itXml4 = xmlList4.iterator();
			List mapList4 = new ArrayList();
			Map map4 = new HashMap();
			// XML 데이터 
			while (itXml4.hasNext()) {
				Element el = (Element)itXml4.next();
				map4 = new HashMap();
				map4.put("CODE", el.getChild("CODE").getText());
				map4.put("NAME", el.getChild("NAME").getText());
				mapList4.add(map4);
			}		
			modelAndView.addObject("mapList4", mapList4); //장소2
			modelAndView.addObject("viewMap", map);
			modelAndView.addObject("mapList2", mapList2);
		}
		// 리턴
		return modelAndView;
	}
}